//hammi a hammbruari clone in C++
//andyp 03.07.12

#include <iostream>
#include <time.h>
#include <stdlib.h>

using namespace std;

int rand_function(int seed);

int main()
{
  int chance;  
  //initialise main variables
  int year = 1;
  int population= 95;
  int starved=0;
  int storage = 2800;
  int harvest = 3000;
  int eaten= 0;//harvest - staorage
  int yield=3;
  int acres = 1000; //harvest/yield
  int immigrants = 5;
  int land_cost = 17;
  int acres_to_buy = 0;
  int acres_to_sell = 0;
  int bflag = 0;
  char any;//for mckenna
  // main loop
  do
    {
      //output screen
      cout << "In the year " << year << endl;
      cout << "our population is " << population << endl;
      cout << "we own " << acres << " acres" << endl;
      cout << "rats ate " << eaten << " bushels" << endl;
      cout << "We have " << storage << " bushels in storage" << endl;
      cout << " we had " << starved  << " citizens starve" << endl;
      cout << " we had " << immigrants << " enter our city" << endl;
      ++year;
      ++population;
      //cost of land
      land_cost=land_cost+rand_function(10);
      //buy loop
      cout << "Do you wish to buy some land, it is trading at " << land_cost << " bushels per acre" << endl;
      cout << "enter number of acres to buy or 0 for none ";
      cin >> acres_to_buy;
      while((bflag== 0) && (acres_to_buy !=0))
	{
	  if ((acres_to_buy*land_cost)<storage)
	    {
	      bflag=1;
	      storage = storage-(acres_to_buy*land_cost);
	    }
	  else
	    {
	      cout << " you don't have enough bushels for that amount" << endl;
	      cout << " try again ";
	      bflag =0;
	      cin >> acres_to_buy;
	    }
	}
      cout << "you bought " << acres_to_buy << " acres" << endl;
      acres=acres+acres_to_buy;
      //sell loop
      cout << "do you wish to sell some land ?, land is trading at " << land_cost << " bushels per Acre" << endl;
      cout<< "Enter number of Acres to sell or 0 for none ";
      cin >> acres_to_sell;
      bflag = 0;
      while((bflag==0) && (acres_to_sell != 0))
        {
          if ((acres_to_sell <= acres))
            {
              bflag =1;
              storage+=(acres_to_sell*land_cost);
              acres-=acres_to_sell;
            }
          else 
            {
              cout << "you don't have that many acres to sell " << endl;
              cout << "try again ";
              bflag=0;
              cin >> acres_to_sell;
            }
        }
    
      // TODO plague check
      if (rand_function(100) < 15)
	{
	  population = population /2;
	  cout << " Your people have been affected by the curse of Bob " << population << " citizens died" << endl;
	}
      // rat check
      if(rand_function(100) < 10)
        {
          eaten = storage* ((rand_function(30) + 80));
          cout << "really bad luck rats have eaten " << eaten << " bushels of food";
          storage=storage-eaten;
        }
 
      cout << "Press any Key To continue";
      cin >> any;
    }
  while (year < 10);
    
  return 0;
}
//TODO:
int rand_function(int seed)
{
  srand(time(NULL));
  return(rand()%seed);
}
